﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace wlm.wsx.src.wlm.Newwsx
{
	public class Parser
    {
 
        /// <summary>
        /// 解析xml文档返回结果
        /// </summary>
        /// <param name="xmlDocument">需要解析的xml文件</param>
        /// <returns></returns>
        public string Parse(System.Xml.XmlDocument xmlDocument)
        {
            if (xmlDocument == null)
                return string.Empty;

             // 返回用，变量对象
            Element element = null;

            CommonData.ElementList.Clear();

            this.GetOutPut(xmlDocument.DocumentElement,ref element);
 
            if (element == null)
                return string.Empty;

            return element.ToString();
        }

        /// <summary>
        /// 循环执行输出
        /// </summary>
        /// <param name="node"></param>
        /// <param name="element"></param>
        private void GetOutPut(XmlNode node, ref Element element)
        {
            // 检查是否还有嵌套,do,if,operator
            if (node.SelectNodes("do | if | operator").Count > 0)
            {
                // 继续递归查找下面
                for (int index = 0; index < node.ChildNodes.Count; index++)
                {
                    this.GetOutPut(node.ChildNodes[index], ref element);
                }
            }
            //if (node.ParentNode.NodeType == XmlNodeType.Document)
            //    return;
             // 没有嵌套则执行
            Execute(node, ref element);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="exeNode">需执行的节点</param>
        /// <param name="element"></param>
        private void Execute(XmlNode exeNode, ref Element element)
        {
            if (exeNode == null)
                return;
            // 执行计算，程序块，判断
            switch (exeNode.Name.ToUpper())
            {
                case CommonData._OPERATOR: // 计算
                    Operator(exeNode, ref element);
                    break;
                case CommonData._PROGRAM:
                    break;
                case CommonData._BOOL:
                case CommonData._FLOAT:
                case CommonData._DOUBLE:
                case CommonData._STRING:
                case CommonData._INT: // 变量添加
                    CommonData.AddElement(exeNode, ref element);
                    break;
            }
          }

        /// <summary>
        /// 执行计算
        /// </summary>
        /// <param name="operNode"></param>
        /// <param name="element"></param>
        private void Operator(XmlNode operNode, ref Element element)
        {
            try
            {
                // 根节点是operator使用
                CommonData.Operator(operNode, ref element);
                element.SetXmlNode(operNode.OwnerDocument);

                // 到这执行计算，直到最上层，node.ParentNode为止，
                // 用当前返回的结果替换node.ParentNode节点中的operator，一直替换上去，直到为null
                if (operNode.ParentNode != null )//&& operNode.ParentNode.ParentNode.NodeType != XmlNodeType.Document)
                {
                    operNode.ParentNode.InsertBefore(element.Node, operNode);
                    XmlNode xmlParent = operNode.ParentNode;
                    xmlParent.RemoveChild(operNode);
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
      }
}
